package com.kuqi.controller;

import com.kuqi.Enumeration.ResponseCode;
import com.kuqi.dao.ChapterRepository;
import com.kuqi.entity.ChapterInfo;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ChapterController {

    @Autowired
    ChapterRepository chapterRepository;

    //根据书籍id查询所有章节（不带内容）
    @ResponseBody
    @RequestMapping(value = "/app/findAllChaptersByBookId.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String findAllChaptersByBookId(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        int bookId = Integer.parseInt(jsonParam.get("bookId").toString());
        List<ChapterInfo> chapters = chapterRepository.findAllChaptersByBookId(bookId);
        JSONObject result = new JSONObject();
        if(chapters == null || chapters.size() <= 0){ // 查询不到
            result.put("code", ResponseCode.NORESULT.getCode());
            result.put("msg",ResponseCode.NORESULT.getValue());
            result.put("data",null);
        }else{
            result.put("code",ResponseCode.SUCCESS.getCode());
            result.put("msg",ResponseCode.SUCCESS.getValue());
            JSONArray arr = new JSONArray();
            for(ChapterInfo item : chapters){
                arr.add(item.toJsonObject());
            }
            result.put("data",arr);
        }
        return result.toJSONString();
    }


    //根据书籍id和index查询指定章节
    @ResponseBody
    @RequestMapping(value = "/app/findChapterWithIndex.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String findChapterWithIndex(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        int bookId = Integer.parseInt(jsonParam.get("bookId").toString());
        int chapterIndex = Integer.parseInt(jsonParam.get("chapterIndex").toString());
        ChapterInfo chapter = chapterRepository.findChapterWithIndex(bookId, chapterIndex);
        JSONObject result = new JSONObject();
        if(chapter == null){ // 查询不到
            result.put("code", ResponseCode.NORESULT.getCode());
            result.put("msg",ResponseCode.NORESULT.getValue());
            result.put("data",null);
        }else{
            result.put("code",ResponseCode.SUCCESS.getCode());
            result.put("msg",ResponseCode.SUCCESS.getValue());
            JSONArray arr = new JSONArray();
            arr.add(chapter.toJsonObject());
            result.put("data",arr);
        }
        return result.toJSONString();
    }


    //根据书籍id和范围查询一部分章节
    @ResponseBody
    @RequestMapping(value = "/app/findChaptersWithSubsection.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String findChaptersWithSubsection(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        int bookId = Integer.parseInt(jsonParam.get("bookId").toString());
        int maxIndex = Integer.parseInt(jsonParam.get("maxIndex").toString());
        int minIndex = Integer.parseInt(jsonParam.get("minIndex").toString());
        List<ChapterInfo> chapters = chapterRepository.findChaptersWithSubsection(bookId, minIndex, maxIndex);
        JSONObject result = new JSONObject();
        if(chapters == null || chapters.size() <= 0){ // 查询不到
            result.put("code", ResponseCode.NORESULT.getCode());
            result.put("msg",ResponseCode.NORESULT.getValue());
            result.put("data",null);
        }else{
            result.put("code",ResponseCode.SUCCESS.getCode());
            result.put("msg",ResponseCode.SUCCESS.getValue());
            JSONArray arr = new JSONArray();
            for(ChapterInfo item : chapters){
                arr.add(item.toJsonObject());
            }
            result.put("data",arr);
        }
        return result.toJSONString();
    }



}